home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 4 / The 640 Meg Shareware Studio CD-ROM Volume IV (Data Express)(1994).ISO / clang / xlib05.zip / XFILEIO.ASM < prev    next >
Assembly Source File  |  1993-08-25  |  11KB  |  434 lines

  1. ;-----------------------------------------------------------------------
  2. ; MODULE XFILEIO
  3. ;
  4. ; Sequential binary file I/O functions
  5. ;
  6. ; Some functions based on a r.g.p post by Joshua Jensen
  7. ;
  8. ; Compile with Tasm.
  9. ; C callable.
  10. ;
  11. ;
  12. ; ****** XLIB - Mode X graphics library                ****************
  13. ; ******                                               ****************
  14. ; ****** Written By Themie Gouthas                     ****************
  15. ;
  16. ; egg@dstos3.dsto.gov.au
  17. ; teg@bart.dsto.gov.au
  18. ;-----------------------------------------------------------------------
  19. COMMENT $
  20.  
  21.  
  22. $
  23.  
  24. LOCALS
  25. .286
  26.  
  27. include model.inc
  28. include xfileio.inc
  29.     .data
  30.  
  31.     _file_err dw (?)  ; error value
  32.  
  33.     .code
  34.  
  35. PUSH_DS macro
  36.     IFNDEF s
  37.     push  ds
  38.     ENDIF
  39.     endm
  40.  
  41. POP_DS macro
  42.     IFNDEF s
  43.     pop   ds
  44.     ENDIF
  45.     endm
  46.  
  47. LDS_M macro arg1,arg2
  48.     IFNDEF s
  49.     lds &arg1&,&arg2&
  50.     ELSE
  51.     mov &arg1&,word ptr &arg2&
  52.     ENDIF
  53.     endm
  54.  
  55.  
  56.  
  57. ;****************************************************************
  58. ;
  59. ; name: f_open
  60. ;
  61. ; C Prototype:
  62. ;
  63. ;     extern int f_open(char * filename, char access)
  64. ;
  65. ; Opens a file according to the access char:
  66. ;
  67. ;   0 = read only   - If doesnt exist return error
  68. ;   1 = write only  - If doesnt exist create it otherwise clear it
  69. ;   2 = read/write  - If doesnt exist create it
  70. ;
  71. ; Returns the file handle on success, -1 on failure
  72. ;
  73. ;
  74. proc _f_open
  75. IFNDEF s
  76.   ARG   filename:dword,access:byte
  77. ELSE
  78.   ARG   filename:word,access:byte
  79. ENDIF
  80.     push  bp             ; Preserve caller's stack frame
  81.     mov   bp,sp
  82.     PUSH_DS
  83.     LDS_M dx,[filename]  ; point DS:DX to file name string
  84.     mov  [_file_err],0
  85.     cmp   [access],1
  86.     je    @@creat
  87.  
  88.     mov   ah,3dh         ; select "open file" DOS service
  89.     mov   al,[access]    ; select access type code
  90.     int   21h            ; call DOS service
  91.     jnb   @@Done         ; If carry flag set we have failed
  92.  
  93.  
  94.     cmp   [access],2
  95.     jne   @@error
  96. @@creat:
  97.     mov   ah,3ch         ; select "creat file" DOS service
  98.     mov   cx,0
  99.     int   21h            ; call DOS service
  100.     jnb   @@Done         ; If carry flag set we have failed
  101. @@error:
  102.     mov   [_file_err],ax
  103.     mov   ax,-1          ;  indicate failure
  104. @@Done:                      ; otherwise return file handle
  105.     POP_DS
  106.     pop  bp              ;restore caller's stack frame
  107.     ret
  108. _f_open endp
  109.  
  110.  
  111. ;****************************************************************
  112. ;
  113. ; name: f_close
  114. ;
  115. ; C Prototype:
  116. ;
  117. ;     extern int f_close(int handle)
  118. ;
  119. ; Closes the file associated with the specified handle
  120. ;
  121. ; Returns 0 on success, -1 on failure
  122. ;
  123. proc _f_close
  124. ARG   handle:word
  125.     push bp             ; Preserve caller's stack frame
  126.     mov  bp,sp
  127.  
  128.         mov  [_file_err],0  ; Clear error
  129.     mov  ah,3eh         ; select  "close file handle" DOS service
  130.     mov  bx,[handle]    ; select handle of file to close
  131.     int  21h            ; call DOS service
  132.     jnb  @@Fix          ; failed if carry flag set
  133.     mov   [_file_err],ax;
  134.     mov  ax,-1          ;  return error
  135.     jmp  short @@Done
  136. @@Fix:                      ; otherwise
  137.     xor  ax,ax          ;  return 0
  138. @@Done:
  139.     pop  bp             ;restore caller's stack frame
  140.     ret
  141. _f_close endp
  142.  
  143.  
  144. ;****************************************************************
  145. ;
  146. ; name: f_read
  147. ;
  148. ; C Prototype:
  149. ;
  150. ;     extern int f_read(int handle, char  *buffer, int count)
  151. ;
  152. ; Reads a block of count bytes from the file specified by the handle
  153. ; into the buffer
  154. ;
  155. ; Returns count on success, failure is detectable via _file_err
  156. ;
  157. proc _f_read
  158. IFNDEF s
  159.   ARG   handle:word,buffer:dword,count:word
  160. ELSE
  161.   ARG   handle:word,buffer:word,count:word
  162. ENDIF
  163.     push bp             ; Preserve caller's stack frame
  164.     mov  bp,sp
  165.     PUSH_DS
  166.  
  167.         mov   [_file_err],0  ; Clear error
  168.     mov   ah,3fh         ; select "read from file or device" DOS service
  169.     mov   bx,[handle]    ; select handle of file to close
  170.     mov   cx,[count]
  171.     LDS_M dx,[buffer]
  172.     int   21h            ; call DOS service
  173.     jnb   @@Done         ; failed if carry flag set
  174.     mov   [_file_err],ax
  175.     xor   ax,ax          ;  return error
  176.     jmp   short @@Done
  177. @@Done:
  178.     POP_DS
  179.     pop  bp             ;restore caller's stack frame
  180.     ret
  181. _f_read endp
  182.  
  183. ;****************************************************************
  184. ;
  185. ; name: f_write
  186. ;
  187. ; C Prototype:
  188. ;
  189. ;     extern int f_write(int handle, char *buffer, int count)
  190. ;
  191. ; Writes a block of count bytes to the file specified by the handle
  192. ; from the buffer
  193. ;
  194. ; Returns count on success, error is indicated by _file_err iff count = 0
  195. ;
  196. proc _f_write
  197. IFNDEF s
  198.   ARG   handle:word,buffer:dword,count:word
  199. ELSE
  200.   ARG   handle:word,buffer:word,count:word
  201. ENDIF
  202.     push bp             ; Preserve caller's stack frame
  203.     mov  bp,sp
  204.     PUSH_DS
  205.  
  206.         mov   [_file_err],0  ; Clear error
  207.     mov   ah,40h         ; select "write to file or device" DOS service
  208.     mov   bx,[handle]    ; select handle of file to write
  209.     mov   cx,[count]
  210.     LDS_M dx,[buffer]
  211.     int   21h            ; call DOS service
  212.     jnb   @@Done         ; has the function failed ?
  213.     mov   [_file_err],ax
  214.     xor   ax,ax          ;  yes, return error
  215.     jmp   short @@Done
  216. @@Done:                      ; otherwise return bytes written
  217.     POP_DS
  218.     pop  bp              ; restore caller's stack frame
  219.     ret
  220. _f_write endp
  221.  
  222. ;****************************************************************
  223. ;
  224. ; name: f_readfar
  225. ;
  226. ; C Prototype:
  227. ;
  228. ;     extern int f_readfar(int handle, char far * buffer, int count)
  229. ;
  230. ; Reads a block of count bytes from the file specified by the handle
  231. ; into the buffer
  232. ;
  233. ; Returns count on success, failure is detectable via _file_err
  234. ;
  235. proc _f_readfar
  236. ARG   handle:word,buffer:dword,count:word
  237.     push bp             ; Preserve caller's stack frame
  238.     mov  bp,sp
  239.     push ds
  240.  
  241.         mov  [_file_err],0  ; Clear error
  242.     mov  ah,3fh         ; select "read from file or device" DOS service
  243.     mov  bx,[handle]    ; select handle of file to close
  244.     mov  cx,[count]
  245.     lds  dx,[buffer]
  246.     int  21h            ; call DOS service
  247.     jnb  @@Done         ; failed if carry flag set
  248.     mov  [_file_err],ax
  249.     xor  ax,ax          ;  return error
  250.     jmp  short @@Done
  251. @@Done:
  252.     pop  ds
  253.     pop  bp             ;restore caller's stack frame
  254.     ret
  255. _f_readfar endp
  256.  
  257. ;****************************************************************
  258. ;
  259. ; name: f_writefar
  260. ;
  261. ; C Prototype:
  262. ;
  263. ;     extern int f_writefar(int handle, char far * buffer, int count)
  264. ;
  265. ; Writes a block of count bytes to the file specified by the handle
  266. ; from the buffer
  267. ;
  268. ; Returns count on success, error is indicated by _file_err iff count = 0
  269. ;
  270. proc _f_writefar
  271. ARG   handle:word,buffer:dword,count:word
  272.     push bp             ; Preserve caller's stack frame
  273.     mov  bp,sp
  274.     push ds
  275.  
  276.         mov  [_file_err],0  ; Clear error
  277.     mov  ah,40h         ; select "write to file or device" DOS service
  278.     mov  bx,[handle]    ; select handle of file to write
  279.     mov  cx,[count]
  280.     lds  dx,[buffer]
  281.     int  21h            ; call DOS service
  282.     jnb  @@Done         ; has the function failed ?
  283.     mov  [_file_err],ax
  284.     xor  ax,ax          ;  yes, return error
  285.     jmp  short @@Done
  286. @@Done:                     ; otherwise return bytes written
  287.     pop  ds
  288.     pop  bp             ; restore caller's stack frame
  289.     ret
  290. _f_writefar endp
  291.  
  292.  
  293.  
  294. ;****************************************************************
  295. ;
  296. ; name: f_seek
  297. ;
  298. ; C Prototype:
  299. ;
  300. ;   extern long int f_seek(int handle, long int position, char method_code)
  301. ;
  302. ; Moves the file pointer according to the position and method code
  303. ;
  304. ; Returns file pointer position on success, -1 on failure
  305. ;
  306. proc _f_seek
  307. ARG   handle:word,position:dword,method_code:byte
  308.     push bp             ; Preserve caller's stack frame
  309.     mov  bp,sp
  310.  
  311.         mov  [_file_err],0  ; Clear error
  312.     mov  ah,42h         ; select "move file pointer" DOS service
  313.     mov  bx,[handle]    ; select handle of file to close
  314.     mov  al,[method_code]
  315.     mov  cx,word ptr [position+2]
  316.     mov  dx,word ptr [position]
  317.     int  21h            ; call DOS service
  318.     jnb  @@Done         ; has the function failed ?
  319.     mov  [_file_err],ax
  320.     mov  ax,-1          ;  yes, return error
  321.     mov  dx,-1          ;
  322.     jmp  short @@Done
  323. @@Done:                     ; otherwise return bytes written
  324.     pop  bp             ; restore caller's stack frame
  325.     ret
  326. _f_seek endp
  327.  
  328. ;****************************************************************
  329. ;
  330. ; name: f_tell
  331. ;
  332. ; C Prototype:
  333. ;
  334. ;   extern long int f_tell(int handle)
  335. ;
  336. ; Returns file pointer position on success, -1 on failure
  337. ;
  338. proc _f_tell
  339. ARG   handle:word,position:dword,method_code:byte
  340.     push bp             ; Preserve caller's stack frame
  341.     mov  bp,sp
  342.  
  343.     mov  [_file_err],0  ; Clear error
  344.     mov  ah,42h         ; select "move file pointer" DOS service
  345.     mov  bx,[handle]    ; select handle of file to close
  346.     xor  dx,dx
  347.     mov  cx,dx
  348.     int  21h
  349.     jnb  @@Done
  350.     mov  [_file_err],ax
  351.     mov  ax,-1          ;  yes, return error
  352.     mov  dx,-1          ;
  353.     jmp  short @@Done
  354. @@Done:                     ; otherwise return bytes written
  355.     pop  bp             ; restore caller's stack frame
  356.     ret
  357. _f_tell endp
  358.  
  359.  
  360. ;****************************************************************
  361. ;
  362. ; name: f_filelength
  363. ;
  364. ; C Prototype:
  365. ;
  366. ;   extern long int f_filelength(int handle)
  367. ;
  368. ; Returns the length of the file associated with the specified handle
  369. ;
  370. ; Returns file length on success, -1 on failure
  371. ;
  372. proc _f_filelength
  373. ARG     handle:word
  374. LOCAL   low:word,high:word=LocalStk
  375.     push bp             ; Preserve caller's stack frame
  376.     mov  bp,sp
  377.     sub  sp,LocalStk
  378.  
  379.     mov  [_file_err],0  ; Clear error
  380.  
  381.     ; Get ptr's current location in file and save it
  382.  
  383.     mov  ah,42h         ; select "move file pointer" DOS service
  384.     mov  al,1           ; select "from current location" method
  385.     mov  bx,[handle]    ; select handle of file to close
  386.     xor  cx,cx
  387.     xor  dx,dx
  388.     int  21h
  389.     jb   @@Error
  390.     mov  [low],ax
  391.     mov  [high],dx
  392.  
  393.     ; Get ptr's value at end of file
  394.  
  395.     mov  ah,42h         ; select "move file pointer" DOS service
  396.     mov  al,2           ; select "from end of file" method
  397.     mov  bx,[handle]    ; select handle of file to close
  398.     xor  cx,cx
  399.     xor  dx,dx
  400.     int  21h
  401.     jb   @@Error
  402.  
  403.     ; Save the results while returning pointer to its previous location
  404.  
  405.     push ax
  406.     push dx
  407.  
  408.     mov  ah,42h         ; select "move file pointer" DOS service
  409.     mov  al,0           ; select "from start of file" method
  410.     mov  bx,[handle]    ; select handle of file to close
  411.     mov  cx,[high]
  412.     mov  dx,[low]
  413.     int  21h
  414.  
  415.     ; restore resultant length
  416.  
  417.     pop  dx
  418.     pop  ax
  419.  
  420.     jnb   @@Done        ; Was the operation a success ?
  421. @@Error:
  422.     mov  [_file_err],ax
  423.     mov  ax,-1          ;  no, return error
  424.     mov  dx,-1          ;
  425. @@Done:                     ; otherwise return bytes written
  426.     mov  sp,bp
  427.     pop  bp             ; restore caller's stack frame
  428.     ret
  429. _f_filelength endp
  430.  
  431.  
  432.     end
  433.  
  434.